package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.OrderAffirm.OrderAffirm;
import com.banzhi.shop.entity.shoppingcart.Order;
import com.banzhi.shop.model.CallBack.OrderAffirmCartCallBack;
import com.banzhi.shop.model.IOrderAffirmCartModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/11/10.
 */

public class ChoiceOrderAffirmCartImpl implements IOrderAffirmCartModel {
    @Override
    public void zhifu(String address_id, String remarks, final OrderAffirmCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceOrderSubmitUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("address_id", address_id);
        if (remarks != null && !"".equals(remarks)) {
            params.addBodyParameter("remarks", remarks);
        }
        LogUtils.d("OrderAffirmCartImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        OrderAffirm affirm = gson.fromJson(result, OrderAffirm.class);
                        callBack.onzhifuSuccess(affirm);
                    } else {
                        callBack.onzhifuError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("OrderAffirmCartImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(285)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void QueRenOrder(String address_id, final OrderAffirmCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceOrderPreCreateUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("address_id", address_id);
        LogUtils.d("QueRenOrder", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Order order = gson.fromJson(result, Order.class);
                        callBack.QueRenOrderSuccess(order.getRes());
                    } else {
                        callBack.QueRenOrderError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("QueRenOrder", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(274)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
